{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e67abf13-09f9-45ec-aa3d-42fa97763da5",
   "metadata": {},
   "source": [
    "# 第三章 提示优化工具"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "592472ce-68ce-4c7f-8036-9b1b3d078175",
   "metadata": {},
   "source": [
    "<div class=\"toc\">\n",
    " <ul class=\"toc-item\">\n",
    "     <li><span><a href=\"#一、引言\" data-toc-modified-id=\"一、引言\">一、引言</a></span></li>\n",
    "     <li>\n",
    "         <span><a href=\"#二、优化 Prompt 的代码示例\" data-toc-modified-id=\"二、优化 Prompt 的代码示例\">二、优化 Prompt 的代码示例</a></span>\n",
    "         <ul class=\"toc-item\">\n",
    "             <li><span><a href=\"##2.1 示例 1\" data-toc-modified-id=\"2.1 示例 1\">2.1 示例 1</a></span></li>\n",
    "             <li><span><a href=\"##2.2 示例 2\" data-toc-modified-id=\"2.2 示例 2\">2.2 示例 2</a></span></li>\n",
    "             <li><span><a href=\"##2.3 示例 3\" data-toc-modified-id=\"2.3 示例 3\">2.3 示例 3</a></span></li>\n",
    "             </ul>\n",
    "         </li>\n",
    "     <li><span><a href=\"#三、总结与讨论\" data-toc-modified-id=\"三、总结与讨论\">三、总结与讨论</a></span></li>\n",
    "     </ul>\n",
    "</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd7ae329-4590-4b65-ad5b-aa6c63189d91",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 一、引言\n",
    "***\n",
    "\n",
    "对于大语言模型（Large Language Models, LLMs），**我们通常需要遵循一些原则和策略来创建能产生高质量回答的有效提示：**\n",
    "\n",
    "1. 明确和具体：你应该尽可能明确和具体地描述你希望 LLM 执行的任务。如果你期望得到特定类型的回答，应在提示中明确指出。同时，如果存在任何具体的限制或要求，也应明确提出。\n",
    "\n",
    "2. 开放式与封闭式：你可以根据需求选择开放式问题（允许多种回答）或封闭式问题（限制回答范围）。这两种类型的问题各有优势，你应根据实际需求进行选择。\n",
    "\n",
    "3. 语境清晰：为了让 LLM 生成更有意义的回答，你需要确保提供足够的上下文。如果提示是基于之前的信息，那么这些信息应被包含在内。\n",
    "\n",
    "4. 创造力和想象力：如果你期望得到创新性的输出，可以鼓励人工智能进行发散思维或头脑风暴。如果这符合你的需求，你还可以建议人工智能模拟某些场景。\n",
    "\n",
    "**实际上，我们可以利用这些原则和策略来有效地优化提示。**\n",
    "\n",
    "本章将介绍一个适用于 LLM 的优化提示工具。这个工具能够帮助你像 [PromptPerfect](https://promptperfect.jina.ai/home) 那样有效地优化提示，从而最大限度地发挥 LLM 的潜力，并获得更准确、与上下文相关的回答。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe163085-5e4c-45ec-8f7c-bbad5d0cb1bb",
   "metadata": {},
   "source": [
    "## 二、优化 Prompt 的代码示例\n",
    "***\n",
    "\n",
    "注意：我使用的测试环境是 Anaconda + Python 3.8.10。Python 的版本要求 Python 3.7+。\n",
    "\n",
    "开始实验之前，我们需要安装好本次实验所需的第三方依赖库：`openai`：OpenAI Python 库提供了一个便捷的途径，让任何 Python 3.7+ 应用程序可以访问 OpenAI REST API。你可以通过运行以下命令来安装：`pip install openai==1.10.0`。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d2b18e9-d707-4bf1-bc21-180261ef5935",
   "metadata": {},
   "source": [
    "导入此次实验所需的依赖库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6161f2dc-84a1-4ce2-aaf2-be2999a1bfdb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from openai import OpenAI\n",
    "\n",
    "\n",
    "# 从环境变量中获取 OpenAI API Key 或者直接赋值\n",
    "OPENAI_API_KEY = os.environ['OPENAI_API_KEY']\n",
    "OPENAI_API_KEY = \"sk-...你的 OpenAI API Key\"\n",
    "\n",
    "# 如果您使用的是官方 API，就直接用 https://api.openai.com/v1 就行。\n",
    "BASE_URL = \"https://api.openai.com/v1\"\n",
    "# 如果您使用的不是官方 API，而是通过代理进行请求，请设置您的代理 URL。\n",
    "# BASE_URL = \"https://api.xxx.../v1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ddb67b03-9216-4e2d-93ef-d3b0f0e26724",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实例化 OpenAI 对象\n",
    "# 传入参数：OpenAI API Key（必需）、Base URL 和最大重试次数\n",
    "client = OpenAI(api_key=OPENAI_API_KEY, base_url=BASE_URL, max_retries=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fc0a7c50-c6d3-4dd3-923f-b62e4d81bf85",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 参数 n，整数或 Null，可选项，默认为 1。为每条输入信息生成多少个聊天完成选项。\n",
    "# 参数 temperature，实数值或 Null，可选项，默认为 1。使用的采样温度，介于 0 和 2 之间。0.8 等较高值会使输出更加随机，而 0.2 等较低值会使输出更加集中和确定。\n",
    "\n",
    "def get_completions1(system_instruction, llm_prompt, model_endpoint):\n",
    "\n",
    "    response = client.chat.completions.create(model=model_endpoint,\n",
    "                                              messages=[{\"role\": \"system\", \"content\": system_instruction},\n",
    "                                                        {\"role\": \"user\", \"content\": llm_prompt}\n",
    "                                                       ],\n",
    "                                              n=1, temperature=0.60, seed=42,\n",
    "                                              presence_penalty=0, frequency_penalty=0,\n",
    "                                              max_tokens=1024\n",
    "                                             )\n",
    "\n",
    "    return response.choices[0].message.content.strip()\n",
    "\n",
    "\n",
    "def get_completions2(system_instruction, llm_prompt, model_endpoint):\n",
    "\n",
    "    response = client.chat.completions.create(model=model_endpoint,\n",
    "                                              messages=[{\"role\": \"system\", \"content\": system_instruction},\n",
    "                                                        {\"role\": \"user\", \"content\": llm_prompt}\n",
    "                                                       ],\n",
    "                                              n=1, temperature=0, seed=42,\n",
    "                                              presence_penalty=0, frequency_penalty=0,\n",
    "                                              max_tokens=1024\n",
    "                                             )\n",
    "\n",
    "    return response.choices[0].message.content.strip()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35e51e9b-a379-4207-9bed-490e49fcc5b0",
   "metadata": {},
   "source": [
    "下面给出的 `system_instruction` 模板的本质是利用**上下文学习**（Few-shot 演示）和提示工程。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "39e4778e-0213-42ee-abf6-75e0db02d349",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 中文\n",
    "system_instruction1 = f\"\"\"你是一位出色的人工智能提示工程师。你是设计 ChatGPT 提示的专家，能取得最佳效果。\n",
    "\n",
    "要创建能产生高质量回答的有效提示，请考虑以下原则和策略：\n",
    "1. 明确和具体：尽可能明确、具体地说明你希望人工智能做些什么。如果你想要某种类型的答复，请在提示中简要说明。如果有具体的限制或要求，也一定要写明。\n",
    "2. 开放式与封闭式：根据你的需求，你可能会选择开放式问题（允许多种回答）或封闭式问题（缩小可能回答的范围）。两者都有其用途，请根据自己的需要进行选择。\n",
    "3. 语境清晰：确保提供足够的上下文，以便人工智能生成有意义的相关回复。如果提示是基于先前的信息，则应确保包含这些信息。\n",
    "4. 创造力和想象力：如果你想要创造性的输出，可以鼓励人工智能发散思维或进行头脑风暴。如果符合你的需求，你甚至可以建议人工智能想象某些场景。\n",
    "\n",
    "你的任务是根据用户给定的提示进行优化，设计出一个新的、经过优化的 ChatGPT 提示。\n",
    "\n",
    "下面提供了一些好的示例，供你参考：\n",
    "原始提示：你的任务是使用 Python，根据用户给的数据绘制散点图。\n",
    "优化后的提示：你的任务是使用 Python 编程语言，根据用户给定的数据绘制一个散点图。请确保散点图能够清晰地展示数据之间的关系，并使用适当的颜色、形状和标签来区分不同的数据点。此外，你还需要为图表添加标题、坐标轴标签以及图例（如果适用），以便更好地解释数据的含义。最后，请将生成的散点图保存为一个图像文件，以便用户可以查看和分享。\n",
    "原始提示：你的任务是将用户给定的英文翻译成中文。\n",
    "优化后的提示：请将以下英文文本准确地翻译成中文，保持原文的意思和语境不变。如果英文中包含专有名词或术语，请在翻译时注明其标准中文名称或提供相应的解释。此外，请确保翻译后的中文句子通顺、语法正确，并且符合中文的表达习惯。\n",
    "原始提示：你的任务是指导初学者系统地学习机器学习。\n",
    "优化后的提示：你被要求为一个完全没有机器学习背景的初学者设计一个详细的学习计划。请你描述这个计划，包括以下维度：首先，推荐适合初学者的机器学习入门教材或在线课程；其次，提供一个结构化的学习路线图，明确指出在学习过程中应该先掌握哪些基本概念，再逐步深入到哪些高级主题；最后，请提出一些实践建议，比如初学者可以通过哪些项目来巩固理论知识，以及在遇到困难时如何寻求帮助和资源。请确保你的回答涵盖了机器学习的基础知识、算法理解、编程实践，以及如何解决实际问题等方面。\n",
    "\"\"\"\n",
    "\n",
    "# 英文\n",
    "system_instruction2 = f\"\"\"You are an exceptional AI prompt engineer. \\\n",
    "You are an expert at writing ChatGPT Prompts for best results.\n",
    "\n",
    "To create efficient prompts that yield high-quality responses, \\\n",
    "consider the following principles and strategies:\n",
    "1. Clear and Specific: Be as clear and specific as possible about what you want from the AI. If you want a certain type of response, outline that in your prompt. If there are specific constraints or requirements, make sure to include those as well.\n",
    "2. Open-ended vs. Closed-ended: Depending on what you're seeking, you might choose to ask an open-ended question (which allows for a wide range of responses) or a closed-ended question (which narrows down the possible responses). Both have their uses, so choose according to your needs.\n",
    "3. Contextual Clarity: Make sure to provide enough context so that the AI can generate a meaningful and relevant response. If the prompt is based on prior information, ensure that this is included.\n",
    "4. Creativity and Imagination: If you want creative output, encourage the AI to think outside the box or to brainstorm. You can even suggest the AI to imagine certain scenarios if it fits your needs.\n",
    "\n",
    "Your task is to design a new, optimized ChatGPT prompt based on the user-given prompt.\n",
    "Some good examples are provided below for your reference:\n",
    "Original prompt: Your task is to plot a scatterplot based on data given by the user, using Python.\n",
    "Optimized prompt: Your task is to use the Python programming language to draw a scatterplot based on data given by a user. Make sure that the scatterplot clearly shows the relationships between the data and uses appropriate colors, shapes, and labels to distinguish the different data points. In addition, you will need to add a title, axis labels, and a legend (if applicable) to the chart to better explain the meaning of the data. Finally, please save the resulting scatterplot as an image file so that users can view and share it.\n",
    "Original prompt: Your task is to translate the English text given by the user into Chinese.\n",
    "Optimized prompt: Please translate the following English text accurately into Chinese, keeping the meaning and context of the original text intact. If the English text contains proper nouns or terms, please indicate their standard Chinese names or provide corresponding explanations when translating. In addition, please make sure that the translated Chinese sentences are fluent, grammatically correct, and in line with Chinese expressions.\n",
    "Original prompt: You have been tasked with guiding a beginner to learn machine learning in a systematic way.\n",
    "Optimized prompt: You have been asked to design a detailed learning plan for a beginner who has no machine learning background at all. Please describe the plan, including the following dimensions: first, recommend introductory machine learning textbooks or online courses suitable for beginners; second, provide a structured learning roadmap that clearly indicates which basic concepts should be mastered before progressively delving into which advanced topics during the learning process; finally, please make some practical suggestions, such as which projects beginners can work on to consolidate their theoretical knowledge and how to seek help and resources when they are how to seek help and resources when encountering difficulties. Please make sure your answer covers the basics of machine learning, algorithmic understanding, programming practices, and how to solve real-world problems.\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa608fec-0cad-4158-9735-6c5f1090d080",
   "metadata": {},
   "source": [
    "### 2.1 示例 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "bbf05142-cd98-4229-af2e-8a3c820030a2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始提示：你的任务是使用 Python，根据用户给的数据绘制热力图。\n",
      "\n",
      "优化后的提示：你需要使用 Python 编程语言，根据用户提供的数据生成一个热力图。确保热力图清晰地展示数据之间的关系，使用适当的颜色映射和标签来呈现数据密度和分布情况。请为热力图添加标题、坐标轴标签以及必要的图例，以便用户能够准确理解数据的含义。最后，将生成的热力图保存为图像文件，以便用户查看和分享。\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "原始提示的生成结果：\n",
      "要绘制热力图，你可以使用 Python 中的 Matplotlib 库和 Seaborn 库。首先，确保你已经安装了这两个库。然后，你可以按照以下步骤使用用户提供的数据绘制热力图：\n",
      "\n",
      "1. 导入必要的库：\n",
      "\n",
      "```python\n",
      "import matplotlib.pyplot as plt\n",
      "import seaborn as sns\n",
      "```\n",
      "\n",
      "2. 准备数据。假设用户提供的数据是一个二维数组 `data`：\n",
      "\n",
      "```python\n",
      "data = [[1, 2, 3],\n",
      "        [4, 5, 6],\n",
      "        [7, 8, 9]]\n",
      "```\n",
      "\n",
      "3. 使用 Seaborn 库绘制热力图：\n",
      "\n",
      "```python\n",
      "sns.heatmap(data, annot=True, cmap='coolwarm')\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "这段代码将绘制一个热力图，数据来自于 `data`，并且显示每个单元格的数值。你可以根据实际情况调整数据和热力图的样式。\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "优化后的提示的生成结果：\n",
      "为了生成热力图，你可以使用 Python 中的 Matplotlib 和 Seaborn 库。以下是一个简单的示例代码，演示如何生成一个基本的热力图：\n",
      "\n",
      "```python\n",
      "import numpy as np\n",
      "import seaborn as sns\n",
      "import matplotlib.pyplot as plt\n",
      "\n",
      "# 创建一个随机数据矩阵\n",
      "data = np.random.rand(10, 10)\n",
      "\n",
      "# 使用Seaborn库生成热力图\n",
      "sns.heatmap(data, annot=True, cmap='coolwarm')\n",
      "\n",
      "# 添加标题和标签\n",
      "plt.title('Heatmap of Random Data')\n",
      "plt.xlabel('X Axis Label')\n",
      "plt.ylabel('Y Axis Label')\n",
      "\n",
      "# 显示图例\n",
      "plt.colorbar(label='Color Scale')\n",
      "\n",
      "# 保存热力图为图像文件\n",
      "plt.savefig('heatmap.png')\n",
      "\n",
      "# 显示热力图\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "你可以根据实际数据的格式和需求，修改代码中的数据矩阵和图表设置，以生成符合你要展示数据关系的热力图。最后，通过保存图像文件，你可以方便地分享和查看生成的热力图。\n"
     ]
    }
   ],
   "source": [
    "llm = \"gpt-3.5-turbo-0125\"\n",
    "\n",
    "original_prompt = \"你的任务是使用 Python，根据用户给的数据绘制热力图。\"\n",
    "original_prompt = f\"\"\"原始提示：{original_prompt}\"\"\"\n",
    "\n",
    "optimized_prompt = get_completions1(system_instruction1, original_prompt, llm)\n",
    "print(f\"{original_prompt}\\n\")\n",
    "print(f\"{optimized_prompt}\\n\")\n",
    "print(\"-\" * 100)\n",
    "instruction_ = \"你是世界知识专家。\"\n",
    "result1 = get_completions2(instruction_, original_prompt, llm)\n",
    "result2 = get_completions2(instruction_, optimized_prompt, llm)\n",
    "\n",
    "print(f\"原始提示的生成结果：\\n{result1}\\n\")\n",
    "print(\"-\" * 100)\n",
    "print(f\"优化后的提示的生成结果：\\n{result2}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6e87abb5-acde-4089-9c4c-2f654f7c0b87",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original prompt: Your task is to use Python and draw a heat map based on the data given by the user.\n",
      "\n",
      "Optimized prompt: Your task is to create a heatmap using Python, utilizing the data provided by the user. Ensure that the heatmap is visually clear and informative, with appropriate color gradients to represent the data variations effectively. Additionally, include axis labels, a title, and a color bar legend to aid in interpreting the heatmap accurately. Finally, save the heatmap as an image file for easy sharing and viewing.\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "原始提示的生成结果：\n",
      "To draw a heat map in Python based on user-provided data, you can use libraries such as Matplotlib and Seaborn. Here is an example code snippet that demonstrates how to create a simple heat map using Matplotlib and Seaborn:\n",
      "\n",
      "```python\n",
      "import numpy as np\n",
      "import seaborn as sns\n",
      "import matplotlib.pyplot as plt\n",
      "\n",
      "# User-provided data (example data)\n",
      "data = np.random.rand(5, 5)  # Replace this with the user's data\n",
      "\n",
      "# Create a heatmap using Seaborn\n",
      "sns.heatmap(data, annot=True, cmap='YlGnBu')\n",
      "\n",
      "# Add labels and title\n",
      "plt.xlabel('X-axis')\n",
      "plt.ylabel('Y-axis')\n",
      "plt.title('Heatmap based on user data')\n",
      "\n",
      "# Display the heatmap\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "In this code snippet:\n",
      "1. We import the necessary libraries: NumPy for generating random data, Seaborn for creating the heatmap, and Matplotlib for plotting.\n",
      "2. We generate example random data using NumPy. You should replace this line with the user's data.\n",
      "3. We create a heatmap using Seaborn's `heatmap` function, passing the data, enabling annotations, and specifying a color map.\n",
      "4. We add labels to the x-axis and y-axis, as well as a title to the plot.\n",
      "5. Finally, we display the heatmap using Matplotlib.\n",
      "\n",
      "You can modify this code to accept user input for the data or customize the plot further based on the user's requirements.\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "优化后的提示的生成结果：\n",
      "To create a heatmap using Python with the provided data, you can use libraries such as Matplotlib and Seaborn. Here is a sample code snippet that demonstrates how to achieve this:\n",
      "\n",
      "```python\n",
      "import numpy as np\n",
      "import seaborn as sns\n",
      "import matplotlib.pyplot as plt\n",
      "\n",
      "# Sample data provided by the user (replace with actual data)\n",
      "data = np.random.rand(10, 10)\n",
      "\n",
      "# Create the heatmap\n",
      "plt.figure(figsize=(8, 6))\n",
      "sns.heatmap(data, cmap='coolwarm', annot=True, fmt=\".2f\")\n",
      "plt.title('Heatmap of Data')\n",
      "plt.xlabel('X-axis Label')\n",
      "plt.ylabel('Y-axis Label')\n",
      "plt.colorbar(label='Color Scale')\n",
      "\n",
      "# Save the heatmap as an image file\n",
      "plt.savefig('heatmap.png')\n",
      "plt.show()\n",
      "```\n",
      "\n",
      "In this code snippet:\n",
      "- Replace the `data` variable with the actual data provided by the user.\n",
      "- Adjust the figure size, colormap (`cmap`), annotations, format, title, axis labels, and color bar label as needed.\n",
      "- The `plt.savefig('heatmap.png')` line saves the heatmap as an image file named `heatmap.png` in the current directory.\n",
      "\n",
      "Feel free to customize the code further based on your specific data and visualization requirements. Let me know if you need any more assistance!\n"
     ]
    }
   ],
   "source": [
    "llm = \"gpt-3.5-turbo-0125\"\n",
    "\n",
    "# 英文\n",
    "original_prompt = \"Your task is to use Python and draw a heat map based on the data given by the user.\"\n",
    "original_prompt = f\"\"\"Original prompt: {original_prompt}\"\"\"\n",
    "\n",
    "optimized_prompt = get_completions1(system_instruction2, original_prompt, llm)\n",
    "print(f\"{original_prompt}\\n\")\n",
    "print(f\"{optimized_prompt}\\n\")\n",
    "print(\"-\" * 100)\n",
    "instruction_ = \"You are an expert at world knowledge.\"\n",
    "result1 = get_completions2(instruction_, original_prompt, llm)\n",
    "result2 = get_completions2(instruction_, optimized_prompt, llm)\n",
    "\n",
    "print(f\"原始提示的生成结果：\\n{result1}\\n\")\n",
    "print(\"-\" * 100)\n",
    "print(f\"优化后的提示的生成结果：\\n{result2}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43301ed5-d487-4c5c-938a-846e8b9bf34f",
   "metadata": {},
   "source": [
    "### 2.2 示例 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9e2272b0-a1e1-42e2-a529-47ee0256300f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始提示：你的任务是将用户给定的中文翻译成英文。\n",
      "<text>《蝴蝶书》力图帮助大家降低门槛，缩小应用程序和研究之间的差距，使得大模型应用开发变得触手可及。它是一本面向有一定编程基础或实际项目（不一定是算法）经历，对 ChatGPT（或类似模型）感兴趣、想利用相关技术做一些新产品或应用的学习者的书籍，旨在利用 ChatGPT API 开发相关应用。期望把方法传播给更多人，期望新技术的突破能够更多地改善我们所处的世界。</text>\n",
      "\n",
      "优化后的提示：请准确翻译以下中文文本为英文，确保保持原文意思和语境的准确性。如果中文中包含专有名词或术语，请在翻译时注明其标准英文名称或提供相应的解释。此外，请确保翻译后的英文句子通顺、语法正确，并符合英文表达习惯。\n",
      "<text>《蝴蝶书》力图帮助大家降低门槛，缩小应用程序和研究之间的差距，使得大模型应用开发变得触手可及。它是一本面向有一定编程基础或实际项目（不一定是算法）经历，对 ChatGPT（或类似模型）感兴趣、想利用相关技术做一些新产品或应用的学习者的书籍，旨在利用 ChatGPT API 开发相关应用。期望把方法传播给更多人，期望新技术的突破能够更多地改善我们所处的世界。</text>\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "原始提示的生成结果：\n",
      "\"The Butterfly Book\" aims to help reduce barriers, bridge the gap between applications and research, and make the development of large-scale model applications more accessible. It is a book targeted at learners with some programming background or practical project experience (not necessarily in algorithms) who are interested in ChatGPT (or similar models) and want to use related technologies to create new products or applications. The book is designed to guide the development of applications using the ChatGPT API. The goal is to spread the methods to more people and hope that breakthroughs in new technologies can further improve the world we live in.\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "优化后的提示的生成结果：\n",
      "The \"Butterfly Book\" aims to help reduce barriers, narrow the gap between applications and research, and make the development of large-scale model applications more accessible. It is a book for learners with a certain programming foundation or practical project experience (not necessarily algorithms), who are interested in ChatGPT (or similar models) and want to use related technologies to create new products or applications. The book is designed to utilize the ChatGPT API for developing related applications. The goal is to spread the methods to more people and hope that breakthroughs in new technologies can further improve the world we live in.\n"
     ]
    }
   ],
   "source": [
    "llm = \"gpt-3.5-turbo-0125\"\n",
    "\n",
    "original_prompt = \"你的任务是将用户给定的中文翻译成英文。\"\n",
    "original_prompt = f\"\"\"原始提示：{original_prompt}\"\"\"\n",
    "\n",
    "optimized_prompt = get_completions1(system_instruction1, original_prompt, llm)\n",
    "\n",
    "instruction_ = \"你是世界知识专家。\"\n",
    "content = \"\"\"《蝴蝶书》力图帮助大家降低门槛，缩小应用程序和研究之间的差距，使得大模型应用开发变得触手可及。\\\n",
    "它是一本面向有一定编程基础或实际项目（不一定是算法）经历，\\\n",
    "对 ChatGPT（或类似模型）感兴趣、想利用相关技术做一些新产品或应用的学习者的书籍，\\\n",
    "旨在利用 ChatGPT API 开发相关应用。期望把方法传播给更多人，期望新技术的突破能够更多地改善我们所处的世界。\"\"\"\n",
    "original_prompt = original_prompt + \"\\n\" + f\"<text>{content}</text>\"\n",
    "optimized_prompt = optimized_prompt + \"\\n\" + f\"<text>{content}</text>\"\n",
    "print(f\"{original_prompt}\\n\")\n",
    "print(f\"{optimized_prompt}\\n\")\n",
    "print(\"-\" * 100)\n",
    "result1 = get_completions2(instruction_, original_prompt, llm)\n",
    "result2 = get_completions2(instruction_, optimized_prompt, llm)\n",
    "\n",
    "print(f\"原始提示的生成结果：\\n{result1}\\n\")\n",
    "print(\"-\" * 100)\n",
    "print(f\"优化后的提示的生成结果：\\n{result2}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "9c8ca2b0-e9ce-40fd-a7dd-a49005cdc052",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original prompt: Your task is to translate the Chinese given by the user into English.\n",
      "<text>《蝴蝶书》力图帮助大家降低门槛，缩小应用程序和研究之间的差距，使得大模型应用开发变得触手可及。它是一本面向有一定编程基础或实际项目（不一定是算法）经历，对 ChatGPT（或类似模型）感兴趣、想利用相关技术做一些新产品或应用的学习者的书籍，旨在利用 ChatGPT API 开发相关应用。期望把方法传播给更多人，期望新技术的突破能够更多地改善我们所处的世界。</text>\n",
      "\n",
      "Optimized prompt: Your task is to accurately translate the Chinese text provided by the user into English while ensuring that the meaning and context of the original text are preserved. Please make sure that the translated English sentences are fluent, grammatically correct, and effectively convey the intended message of the Chinese text. Additionally, if there are any specific cultural references or nuances in the Chinese text that need to be explained or adapted for an English-speaking audience, please include those in the translation.\n",
      "<text>《蝴蝶书》力图帮助大家降低门槛，缩小应用程序和研究之间的差距，使得大模型应用开发变得触手可及。它是一本面向有一定编程基础或实际项目（不一定是算法）经历，对 ChatGPT（或类似模型）感兴趣、想利用相关技术做一些新产品或应用的学习者的书籍，旨在利用 ChatGPT API 开发相关应用。期望把方法传播给更多人，期望新技术的突破能够更多地改善我们所处的世界。</text>\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "原始提示的生成结果：\n",
      "\"The Butterfly Book\" aims to help everyone reduce barriers, narrow the gap between applications and research, and make the development of large model applications more accessible. It is a book aimed at learners with a certain programming foundation or practical project experience (not necessarily algorithms), interested in ChatGPT (or similar models), and wanting to use related technologies to create new products or applications. The book is designed to develop related applications using the ChatGPT API. The goal is to spread the methods to more people and hope that breakthroughs in new technologies can further improve the world we live in.\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "优化后的提示的生成结果：\n",
      "\"The Butterfly Book\" aims to help everyone lower the threshold, narrow the gap between applications and research, and make the development of large-scale model applications more accessible. It is a book targeted at learners with a certain programming foundation or practical project experience (not necessarily algorithms), who are interested in ChatGPT (or similar models) and want to use related technologies to create new products or applications. The book is designed to utilize the ChatGPT API for application development. The goal is to spread the methods to more people and hope that breakthroughs in new technologies can further improve the world we live in.\n"
     ]
    }
   ],
   "source": [
    "llm = \"gpt-3.5-turbo-0125\"\n",
    "\n",
    "original_prompt = \"Your task is to translate the Chinese given by the user into English.\"\n",
    "original_prompt = f\"\"\"Original prompt: {original_prompt}\"\"\"\n",
    "\n",
    "optimized_prompt = get_completions2(system_instruction2, original_prompt, llm)\n",
    "\n",
    "instruction_ = \"You are an expert at world knowledge.\"\n",
    "content = \"\"\"《蝴蝶书》力图帮助大家降低门槛，缩小应用程序和研究之间的差距，使得大模型应用开发变得触手可及。\\\n",
    "它是一本面向有一定编程基础或实际项目（不一定是算法）经历，\\\n",
    "对 ChatGPT（或类似模型）感兴趣、想利用相关技术做一些新产品或应用的学习者的书籍，\\\n",
    "旨在利用 ChatGPT API 开发相关应用。期望把方法传播给更多人，期望新技术的突破能够更多地改善我们所处的世界。\"\"\"\n",
    "original_prompt = original_prompt + \"\\n\" + f\"<text>{content}</text>\"\n",
    "optimized_prompt = optimized_prompt + \"\\n\" + f\"<text>{content}</text>\"\n",
    "print(f\"{original_prompt}\\n\")\n",
    "print(f\"{optimized_prompt}\\n\")\n",
    "print(\"-\" * 100)\n",
    "result1 = get_completions2(instruction_, original_prompt, llm)\n",
    "result2 = get_completions2(instruction_, optimized_prompt, llm)\n",
    "\n",
    "print(f\"原始提示的生成结果：\\n{result1}\\n\")\n",
    "print(\"-\" * 100)\n",
    "print(f\"优化后的提示的生成结果：\\n{result2}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5fdef6ad-dbd1-4919-b08f-381c6780f2d8",
   "metadata": {},
   "source": [
    "### 2.3 示例 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8d4e848a-42a5-4857-bdb7-e98f2a33002d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始提示：你的任务是指导初学者系统地学习数据科学。\n",
      "\n",
      "优化后的提示：你被要求为一个完全没有数据科学背景的初学者设计一个详细的学习计划。请你描述这个计划，包括以下维度：首先，推荐适合初学者的数据科学入门教材或在线课程；其次，提供一个结构化的学习路线图，明确指出在学习过程中应该先掌握哪些基本概念，再逐步深入到哪些高级主题；最后，请提出一些实践建议，比如初学者可以通过哪些项目来巩固理论知识，以及在遇到困难时如何寻求帮助和资源。请确保你的回答涵盖了数据科学的基础知识、数据清洗、数据分析、机器学习等方面。\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "原始提示的生成结果：\n",
      "学习数据科学是一个系统性的过程，需要掌握一系列基础知识和技能。以下是我建议的学习路径：\n",
      "\n",
      "1. 编程基础：学习编程语言如Python或R，掌握基本语法和数据结构。\n",
      "2. 统计学基础：了解统计学的基本概念，包括概率、假设检验、回归分析等。\n",
      "3. 数据处理：学习数据清洗、转换和处理技术，包括使用Pandas或dplyr等工具。\n",
      "4. 数据可视化：掌握数据可视化技术，如Matplotlib、Seaborn或ggplot2。\n",
      "5. 机器学习：学习机器学习算法和技术，包括监督学习、无监督学习和深度学习。\n",
      "6. 数据分析：学习如何应用统计和机器学习技术进行数据分析和建模。\n",
      "7. 实践项目：通过实际项目来应用所学知识，提升实战能力。\n",
      "\n",
      "建议初学者可以通过在线课程、教科书、博客和实践项目来学习数据科学。同时，参与数据科学社区和论坛，与其他学习者交流经验和解决问题。持续学习和实践是提升数据科学能力的关键。祝你学习顺利！\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "优化后的提示的生成结果：\n",
      "学习数据科学对于初学者来说可能是一个挑战，但通过有计划的学习和实践，你可以逐步掌握这门技能。以下是一个详细的学习计划：\n",
      "\n",
      "### 教材和在线课程推荐：\n",
      "1. **教材**：\n",
      "   - \"Python for Data Analysis\" by Wes McKinney\n",
      "   - \"Introduction to Statistical Learning\" by Gareth James, Daniela Witten, Trevor Hastie, and Robert Tibshirani\n",
      "   - \"Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow\" by Aurélien Géron\n",
      "\n",
      "2. **在线课程**：\n",
      "   - Coursera上的《数据科学导论》（Introduction to Data Science）\n",
      "   - Udemy上的《Python数据科学入门》（Python for Data Science）\n",
      "   - Kaggle的免费数据科学教程和竞赛\n",
      "\n",
      "### 结构化学习路线图：\n",
      "1. **基础知识**：\n",
      "   - 学习Python编程语言和相关库（如NumPy、Pandas、Matplotlib）\n",
      "   - 了解统计学基础知识和概念\n",
      "   - 掌握数据清洗和数据预处理技术\n",
      "\n",
      "2. **数据分析**：\n",
      "   - 学习数据可视化技术和工具\n",
      "   - 掌握探索性数据分析（EDA）方法\n",
      "   - 学习如何提取数据洞见和进行数据解释\n",
      "\n",
      "3. **机器学习**：\n",
      "   - 了解机器学习基本概念和算法（如线性回归、决策树、聚类等）\n",
      "   - 学习如何构建、训练和评估机器学习模型\n",
      "   - 实践项目来应用机器学习算法解决实际问题\n",
      "\n",
      "### 实践建议：\n",
      "1. **项目实践**：\n",
      "   - 参与Kaggle竞赛或其他数据科学项目，以实际数据集进行实践\n",
      "   - 创建个人项目，如数据分析报告或机器学习模型应用\n",
      "\n",
      "2. **寻求帮助**：\n",
      "   - 加入数据科学社区，如Kaggle、GitHub等，与其他学习者交流和分享经验\n",
      "   - 寻找导师或参加数据科学学习群组，获得指导和支持\n",
      "   - 利用在线资源如Stack Overflow、GitHub等解决遇到的问题\n",
      "\n",
      "通过以上学习计划，你可以逐步建立起数据科学的基础知识和技能，并通过实践项目不断提升自己的能力。记得保持耐心和持续学习，数据科学是一个持续发展的领域，不断实践和探索将帮助你不断进步。祝你学习顺利！\n"
     ]
    }
   ],
   "source": [
    "llm = \"gpt-3.5-turbo-0125\"\n",
    "\n",
    "original_prompt = \"你的任务是指导初学者系统地学习数据科学。\"\n",
    "original_prompt = f\"\"\"原始提示：{original_prompt}\"\"\"\n",
    "\n",
    "optimized_prompt = get_completions1(system_instruction1, original_prompt, llm)\n",
    "print(f\"{original_prompt}\\n\")\n",
    "print(f\"{optimized_prompt}\\n\")\n",
    "print(\"-\" * 100)\n",
    "instruction_ = \"你是世界知识专家。\"\n",
    "result1 = get_completions2(instruction_, original_prompt, llm)\n",
    "result2 = get_completions2(instruction_, optimized_prompt, llm)\n",
    "\n",
    "print(f\"原始提示的生成结果：\\n{result1}\\n\")\n",
    "print(\"-\" * 100)\n",
    "print(f\"优化后的提示的生成结果：\\n{result2}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c125af74-24cb-4b08-a270-c23d904578e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original prompt: Your task is to guide beginners through the systematic study of data science.\n",
      "\n",
      "Optimized prompt: You have been tasked with creating a comprehensive guide for beginners who are interested in studying data science systematically. Please outline a detailed roadmap that covers the essential topics and skills they should focus on, starting from the basics and progressing to more advanced concepts. Include recommendations for beginner-friendly resources such as books, online courses, and tutorials. Additionally, suggest practical projects or exercises that can help reinforce their learning and provide tips on how to stay motivated and seek assistance when needed. Make sure to emphasize the importance of hands-on practice, critical thinking, and continuous learning in the field of data science.\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "原始提示的生成结果：\n",
      "To guide beginners through the systematic study of data science, it's important to start with the basics and gradually build up your knowledge and skills. Here is a step-by-step guide to help you get started:\n",
      "\n",
      "1. **Understand the Basics**: Begin by familiarizing yourself with the fundamental concepts of data science, such as what data science is, why it is important, and how it is used in various industries.\n",
      "\n",
      "2. **Learn Programming Languages**: Data science often involves programming languages such as Python and R. Start by learning the basics of these languages and how they are used in data analysis and visualization.\n",
      "\n",
      "3. **Statistics and Mathematics**: Data science heavily relies on statistics and mathematics. Make sure to have a good understanding of key concepts such as probability, hypothesis testing, and linear algebra.\n",
      "\n",
      "4. **Data Wrangling**: Learn how to clean, preprocess, and manipulate data. This involves tasks such as handling missing values, removing duplicates, and transforming data into a suitable format for analysis.\n",
      "\n",
      "5. **Data Visualization**: Understand the importance of data visualization in data science. Learn how to create effective visualizations to communicate insights from data.\n",
      "\n",
      "6. **Machine Learning**: Dive into machine learning algorithms and techniques. Understand how to build and evaluate predictive models using supervised and unsupervised learning methods.\n",
      "\n",
      "7. **Big Data Technologies**: Familiarize yourself with big data technologies such as Hadoop and Spark, which are commonly used for processing and analyzing large datasets.\n",
      "\n",
      "8. **Real-world Projects**: Practice your skills by working on real-world data science projects. This will help you apply your knowledge and gain practical experience.\n",
      "\n",
      "9. **Stay Updated**: Data science is a rapidly evolving field, so make sure to stay updated with the latest trends, tools, and techniques in the industry.\n",
      "\n",
      "10. **Join Data Science Communities**: Engage with data science communities, attend meetups, and participate in online forums to connect with other data science enthusiasts and professionals.\n",
      "\n",
      "Remember, learning data science is a journey that requires dedication, practice, and continuous learning. By following these steps and staying committed to your learning goals, you can build a strong foundation in data science and advance your skills in this exciting field.\n",
      "\n",
      "----------------------------------------------------------------------------------------------------\n",
      "优化后的提示的生成结果：\n",
      "Roadmap for Beginners in Data Science:\n",
      "\n",
      "1. **Introduction to Data Science**\n",
      "   - Understand the basics of data science, its applications, and the role of a data scientist.\n",
      "   - Recommended resources: \"Data Science for Beginners\" by Martin L. Shoemaker, online articles, and introductory courses on platforms like Coursera or edX.\n",
      "\n",
      "2. **Foundational Skills**\n",
      "   - Learn programming languages such as Python and R, as they are widely used in data science.\n",
      "   - Familiarize yourself with data manipulation, cleaning, and visualization techniques.\n",
      "   - Recommended resources: \"Python for Data Analysis\" by Wes McKinney, online tutorials on data manipulation libraries like Pandas and visualization tools like Matplotlib or Seaborn.\n",
      "\n",
      "3. **Statistics and Mathematics**\n",
      "   - Gain a solid understanding of statistical concepts, probability theory, and linear algebra.\n",
      "   - Learn about hypothesis testing, regression analysis, and other statistical methods commonly used in data science.\n",
      "   - Recommended resources: \"Introduction to Statistical Learning\" by Gareth James et al., online courses on statistics and mathematics on Khan Academy or Udemy.\n",
      "\n",
      "4. **Machine Learning**\n",
      "   - Dive into machine learning algorithms such as linear regression, logistic regression, decision trees, and clustering techniques.\n",
      "   - Understand the concepts of supervised and unsupervised learning, model evaluation, and hyperparameter tuning.\n",
      "   - Recommended resources: \"Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow\" by Aurélien Géron, online courses on machine learning on platforms like Coursera or fast.ai.\n",
      "\n",
      "5. **Big Data and Data Engineering**\n",
      "   - Explore tools and technologies for handling big data, such as Hadoop, Spark, and SQL databases.\n",
      "   - Learn about data pipelines, data warehousing, and distributed computing.\n",
      "   - Recommended resources: Online courses on big data technologies on platforms like Udacity or DataCamp, practical projects involving large datasets.\n",
      "\n",
      "6. **Deep Learning and Neural Networks**\n",
      "   - Delve into deep learning concepts, including neural networks, convolutional neural networks (CNNs), and recurrent neural networks (RNNs).\n",
      "   - Understand how to implement deep learning models using frameworks like TensorFlow or PyTorch.\n",
      "   - Recommended resources: \"Deep Learning\" by Ian Goodfellow et al., online courses on deep learning on platforms like Coursera or deeplearning.ai.\n",
      "\n",
      "7. **Practical Projects and Exercises**\n",
      "   - Work on real-world data science projects to apply your knowledge and skills.\n",
      "   - Participate in Kaggle competitions, build a personal portfolio showcasing your projects, and collaborate with other data science enthusiasts.\n",
      "   - Recommended resources: Kaggle datasets and competitions, GitHub for version control and project sharing, online communities like Stack Overflow or Reddit for seeking help and feedback.\n",
      "\n",
      "8. **Continuous Learning and Growth**\n",
      "   - Stay updated with the latest trends and advancements in data science through blogs, research papers, and attending conferences or meetups.\n",
      "   - Engage in lifelong learning by exploring advanced topics like natural language processing (NLP), reinforcement learning, and time series analysis.\n",
      "   - Recommended resources: Data science blogs like Towards Data Science, research papers on arXiv or Google Scholar, attending conferences like NeurIPS or Strata Data Conference.\n",
      "\n",
      "Remember, the key to mastering data science is consistent practice, critical thinking, and a growth mindset. Stay motivated by setting goals, seeking mentorship from experienced professionals, and never hesitating to ask for help when needed. Embrace challenges as opportunities to learn and grow in your data science journey.\n"
     ]
    }
   ],
   "source": [
    "llm = \"gpt-3.5-turbo-0125\"\n",
    "\n",
    "original_prompt = \"Your task is to guide beginners through the systematic study of data science.\"\n",
    "original_prompt = f\"\"\"Original prompt: {original_prompt}\"\"\"\n",
    "\n",
    "optimized_prompt = get_completions1(system_instruction2, original_prompt, llm)\n",
    "print(f\"{original_prompt}\\n\")\n",
    "print(f\"{optimized_prompt}\\n\")\n",
    "print(\"-\" * 100)\n",
    "instruction_ = \"You are an expert at world knowledge.\"\n",
    "result1 = get_completions2(instruction_, original_prompt, llm)\n",
    "result2 = get_completions2(instruction_, optimized_prompt, llm)\n",
    "\n",
    "print(f\"原始提示的生成结果：\\n{result1}\\n\")\n",
    "print(\"-\" * 100)\n",
    "print(f\"优化后的提示的生成结果：\\n{result2}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae89450c-a90e-4517-8be2-321d2e342cb8",
   "metadata": {
    "jp-MarkdownHeadingCollapsed": true,
    "tags": []
   },
   "source": [
    "## 三、总结与讨论\n",
    "***\n",
    "\n",
    "**优化 ChatGPT 的 Prompts 对于提升交互体验和生成结果的质量非常重要。**以下是一些主要的原因：\n",
    "- 提高准确性：通过优化 Prompts，我们可以更准确地引导 ChatGPT 生成我们期望的回答。这对于获取准确、相关和有用的信息至关重要。\n",
    "\n",
    "- 提高效率：优化的 Prompts 可以减少无效或者不相关的回答，从而提高交互的效率。\n",
    "\n",
    "- 增强可控性：通过优化 Prompts，我们可以更好地控制 ChatGPT 的输出，使其更符合用户的需求和期望。\n",
    "\n",
    "- 提升用户体验：优化的 Prompts 可以帮助 ChatGPT 更好地理解用户的需求，从而提供更满意的回答，提升用户体验。\n",
    "\n",
    "总的来说，优化 Prompts 是提高 ChatGPT 性能和用户满意度的关键步骤。它有助于我们更好地利用提示工程（Prompt Engineering），为用户提供高质量的服务。\n",
    "\n",
    "本章将介绍一个适用于 LLM 的优化提示工具。这个工具能够帮助你像 [PromptPerfect](https://promptperfect.jina.ai/home) 那样有效地优化提示，从而最大限度地发挥 LLM 的潜力，并获得更准确、与上下文相关的回答。\n",
    "\n",
    "在实际应用中，为 LLM 提供的演示对于优化 Prompt 的性能具有重要意义。对于特定需求，如果手动构建优质的演示（**成对的原始提示和优化后的提示**）存在困难，我们可以利用一些商业化（非开源）的提示优化工具，如：[PromptPerfect](https://promptperfect.jina.ai/home)、[百度千帆大模型操作台的 Prompt 优化](https://cloud.baidu.com/doc/WENXINWORKSHOP/s/Clommng91)、[讯飞星火的指令优化](https://xinghuo.xfyun.cn/desk)等。这些商业工具通常进行了大量的工程优化，能够适应各种场景，满足用户的特定需求。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc-autonumbering": false,
  "toc-showcode": false,
  "toc-showmarkdowntxt": false
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
